/**
 * 
 */
function check_all(isCheck, target_name){
	var elements = document.getElementsByName(target_name);
	for(var i = 0; i < elements.length; i++) {
		elements[i].checked = isCheck;
	}
}

function showMessageBox(message_id, mode){
	jQuery("#" + message_id).css('display', mode);
}

function hideMessageBox(message_id){
	jQuery("#" + message_id).css('display', "none");
}

function checkElementExist(embedded_id, value, accept_val){
	//check iwth accept val
	if(accept_val != null && accept_val == value)
		return false;
	
	var embedded_list = jQuery("#" + embedded_id).val();
	var arr = embedded_list.split(";");
	
	if(arr.indexOf(value) == -1)
		return false;
	else
		return true;
}

function checkEmptyField(field_id, mode){
	var value = jQuery("#" + field_id).val();
	value = jQuery.trim(value);
	if(value == ""){
		showMessageBox(field_id + "_empty_message", mode);
		
		jQuery("#" + field_id).val(value);
		jQuery("#" + field_id).focus();
		return false;
	}else{
		hideMessageBox(field_id +  "_empty_message");
	}
	
	return true;
}

function checkSunnetEmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@sunnet\.vn$/;
	if (!filter.test(email))
		return false;
	else 
		return true;
}

function checkPhoneNumber(phone){
	var filter_1 = /^([0-9]){10}$/;
	var filter_2 = /^([0-9]){11}$/;
	if (!filter_1.test(phone) && !filter_2.test(phone) )
		return false;
	else 
		return true;
}

function isPositiveNumber(num_str){
	if(isFinite(num_str)){
		if(parseInt(num_str, 10) > 0)
			return true;
		else
			return false;
	}else{
		return false;
	}
}

function showLoading(element_id){
	var html = "<span class='loadingImage'><img src='/images/icon/loading.gif'/></span>";
	jQuery("#" + element_id).append(html);
}

function hideLoading(element_id){
	jQuery("#" + element_id + " span.loadingImage").remove();
}

function initAutocompleteField(element, url, target){
	$(element).autocomplete({
		source:url,
		delay:10,
		select:function(event, ui){
			$(element).val(ui.item.label);
			$(this).next('input').focus();
			if(target != null)
				$(target).val(ui.item.id);
		}
	}).data( "autocomplete" )._renderItem = function( ul, item ) {
			return $( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( "<a>" + item.label + "</a>" )
			.appendTo( ul );
	};	
}

function addLineElement(value, parent){
	//process input
	value = jQuery.trim(value);
	
	//create selector
	var hidden_selector = "#" + parent + "_hidden";
	var parent_selector = "#" + parent;
	var list_selector = "#" + parent + "_list";
	
	var id = $('#remove_button_count').val();
	var element_id = 'element_' + id;
	$('#remove_button_count').val( parseInt(id) + 1);
	
	//check values exist in list or not
	var current_elements = $(hidden_selector).val();

	if(checkElementExit(current_elements, value)){
		$(parent_selector).val('');
		return;
	}else{
		if(current_elements != '')
			current_elements += ',';
		current_elements += value;
		$(hidden_selector).val(current_elements);
	}
	
	var html = '<div class="listItem" id="' + element_id + '">'
			   + '<div class="itemValue">' + value + '</div>'
			   + '<label class="itemRemoveButton">'
			   + '<input title="Delete" type="button" id="'+ element_id +'"/>'
			   + '</label>'
			   + '<input type="hidden" id="' + element_id + '_hidden" value="' + value + '"/>' 
			   + '</div>';
	
	$(list_selector).append(html);

	//set event handler
	var selector = '.itemRemoveButton input#' + element_id;
	$(selector).click(function(){
		deleteElement(element_id, parent);
	});

	//reset language field and set hidden field
	$(parent_selector).val("");
}

function deleteElement(element_id, parent){
	var hidden_selector = "#" + parent + "_hidden";
	
	var selector = '#' + element_id + '_hidden';
	var del_val = $(selector).val();
	var current_elements = $(hidden_selector).val();

	//remove del_val from current_elements
	current_elements = current_elements.replace(del_val + ",", "");
	current_elements = current_elements.replace("," + del_val, "");
	current_elements = current_elements.replace(del_val, "");
	
	$(hidden_selector).val(current_elements);
	$('#'+element_id).remove();
}

function checkElementExit(parent_str, element){
	var str_arr = parent_str.split(",");
	var pos = str_arr.indexOf(element);
	if(pos == -1)
		return false;
	else
		return true;
}

function initElementsList(parent){
	var parent_hidden_init = "#" + parent + "_hidden_init";
	var current_elements = $(parent_hidden_init).val();
	if(current_elements == '')
		return;
	var arr = current_elements.split(',');
	for(i=0; i<arr.length; i++){
		addLineElement(arr[i], parent);
	}
}